package com.bidepan.big_event.controller;

import com.bidepan.big_event.pojo.Result;
import com.bidepan.big_event.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class fileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //file.transferTo把文件内容存储到本地磁盘上
        //获取文件名
        String originlFilname = file.getOriginalFilename();
        String fileName = null;
        //保证文件的名字是唯一的，从而防止重复上传同一文件名的文件发生文件覆盖
        if (originlFilname != null) {
            fileName = UUID.randomUUID().toString()+originlFilname.substring(originlFilname.lastIndexOf("."));
        }
        //file.transferTo(new File("D:\\86151\\javaWeb\\springboot\\big_event\\files\\"+fileName));
        String url = AliOssUtil.uploadFile(fileName,file.getInputStream());
        return Result.success(url);
    }
}
